﻿using System;

namespace SystmicFunction.IRMHelper.RBPS
{
    /// <summary>
    ///     权限助手
    /// </summary>
    public static class RoleHelper
    {
        private const int Basei = 2;
        private const int Maxi = 61;

        /// <summary>
        ///     计算权限总和,数据值范围：0-60
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public static long Sum(long[] x)
        {
            long sum = 0;
            foreach (long t in x)
            {
                if (t > Maxi)
                    throw new Exception("数据值范围：0-60");
                sum += (long) Math.Pow(Basei, t);
            }
            return sum;
        }

        /// <summary>
        ///     验证权限,如果正确表示存在该值，错误反之
        /// </summary>
        /// <param name="currentVa">用户当前权限值</param>
        /// <param name="va">要判断的权限值,范围：0-60</param>
        /// <returns></returns>
        public static bool Validate(long currentVa, long va)
        {
            if (va > Maxi)
                throw new Exception("数据值范围：0-60");
            var val = (long) Math.Pow(Basei, va);
            return (val & currentVa) == val;
        }
    }
}